package cn.ddd.linktran.infrastructure.transform.gateway;

import cn.ddd.linktran.domain.domainobject.UrlMapDO;
import cn.ddd.linktran.domain.gateway.UrlMapGateWay;
import cn.ddd.linktran.infrastructure.token.convertor.UrlMapConvertor;
import cn.ddd.linktran.infrastructure.transform.mapper.UrlMapMapper;
import cn.ddd.linktran.infrastructure.transform.po.UrlMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UrlMapGateWayImpl implements UrlMapGateWay {

    @Autowired
    private UrlMapMapper urlMapMapper;

    @Autowired
    private UrlMapConvertor urlMapConvertor;
    @Override
    public int insertUrlMapDO(UrlMapDO urlMapDO) {
        return urlMapMapper.insert(urlMapConvertor.toPO(urlMapDO));
    }

    @Override
    public UrlMapDO getUrlMapDOByCompressionCode(String compressionCode) {
        UrlMap po = urlMapMapper.selectOne(
                Wrappers.<UrlMap>lambdaQuery()
                        .eq(UrlMap::getCompressionCode, compressionCode)
                        .last("limit 1")
        );
        return po == null ? null : urlMapConvertor.toDO(po);
    }
}
